<script>
	import LoginUtil from '@/main/common/LoginUtil.js'
	import constant from '@/common/constant.js'
	import IMUtil from '@/main/common/IMUtil.js';
	import ConfigUtil from '@/main/common/ConfigUtil.js';
	import HttpUtil from '@/common/HttpUtil.js';
	import toast from '@/main/common/ToastUtil.js';
	uni.$http = HttpUtil;
	uni.$constant = constant;
	uni.$toast = toast;
	
	import TencentCloudChat from '@tencentcloud/chat';
	import { TUIChatKit, genTestUserSig } from "./TUIKit";
	import { TUILogin } from "@tencentcloud/tui-core";
	// IMUtil.init()
	uni.$chat_SDKAppID = constant.imApppId;
	uni.$chat_secretKey = constant.imSecretKey;
	uni.$TUIKit = TencentCloudChat.create({
		SDKAppID: uni.$chat_SDKAppID
	});
	//离线推送
	// uni.$TUIOfflinePush = uni.requireNativePlugin("TencentCloud-TUIOfflinePush");
	// console.warn(uni.$TUIOfflinePush, "TUIOfflinePush ｜ ok");
	

	export default {
		globalData: {
			webUrl: '',
			webTitle: '',
			chatUserId: '',
			chatTxCode: '',
			callerId: '',
			timeCall: {},
			versionName: '',
			openTicketMin: false,
			ticketStartTime: 0,
			ticketTotalTime: 180,
			deviceModel: '',
		},
		onLaunch: function() {
			uni.$online = constant.online;
			uni.getSystemInfo({
				success: (e) => {
					this.globalData.deviceModel = e.brand + '/' + e.deviceModel;
				}
			})
			ConfigUtil.checkUpdate();
			LoginUtil.loginOnce()
			this.loginIM();
			// 离线推送：打开通知栏
			// IMUtil.initOfflinePushListener()
			// uni.$TUIOfflinePush.setOfflinePushListener((data) => {
			//     console.log('打开离线消息', data);
			//     uni.switchTab({
			//         url: "/pages/tab/message/message",
			//     });
			// });
		},

		methods: {
			loginIM() {
				// IMUtil.login()
				const txCode = uni.getStorageSync("userTxCode");
				const token = uni.getStorageSync("token");
				if (txCode && token) {
					uni.$chat_userID = txCode + '';
					const config = {
						userID: uni.$chat_userID,
						SDKAppID: uni.$chat_SDKAppID,
						secretKey: uni.$chat_secretKey,
					}
					uni.$chat_userSig = genTestUserSig(config).userSig;
					TUIChatKit.init();
				
					const options = {
						SDKAppID: uni.$chat_SDKAppID,
						userID: uni.$chat_userID,
						userSig: uni.$chat_userSig,
						useUploadPlugin: true,
						framework: `vue2`,
						// TUIOfflinePush: uni.$TUIOfflinePush, // App 注册离线推送插件
						// offlinePushConfig: {
						// 	// huawei
						// 	huaweiBusinessID: '33207', // 在腾讯云控制台上传第三方推送证书后分配的证书 ID
						// 	// xiaomi
						// 	xiaomiBusinessID: '30110', // 在腾讯云控制台上传第三方推送证书后分配的证书 ID
						// 	xiaomiAppID: '2882303761520247850', // 小米开放平台分配的应用 APPID
						// 	xiaomiAppKey: '5562024796850', // 小米开放平台分配的应用 APPKEY
						// 	// vivo
						// 	vivoBusinessID: '33244', // 在腾讯云控制台上传第三方推送证书后分配的证书 ID
						// 	// oppo
						// 	oppoBusinessID: '33245', // 在腾讯云控制台上传第三方推送证书后分配的证书 ID
						// 	oppoAppKey: '076df83f6ee946b591916e2dd4bb9833', // oppo 开放平台分配的应用 AppKey
						// 	oppoAppSecret: 'cdf8f66a8f3e48f3951d1a1e28f9cd17', // oppo 开放平台分配的应用 AppSecret
						// 	// honor
						// 	honorBusinessID: '', // 在腾讯云控制台上传第三方推送证书后分配的证书 ID
						// 	// ios
						// 	iosBusinessID: '', // 在腾讯云控制台上传第三方推送证书后分配的证书 ID
						// },
					};
					//IM登录
					TUILogin.login(options).then(res => {
						console.log("IM登录成功：" + JSON.stringify(res));
						this.initUnReadMessage();
					}).catch(error => {
						console.log('IM登录失败:' + JSON.stringify(error));
					});
				}
			},
			initUnReadMessage() {
				//初始化未读消息
				setTimeout(()=>{
					uni.$TUIKit.getConversationList().then(res => {
						const unReadMessageCount = uni.$TUIKit.getTotalUnreadMessageCount();
						this.setUnReadMessageCount(unReadMessageCount);
					}).catch(error => {});
				}, 1000)
				
				//监听未读消息数
				uni.$TUIKit.on(TencentCloudChat.EVENT.TOTAL_UNREAD_MESSAGE_COUNT_UPDATED, event => {
					this.setUnReadMessageCount(event.data);
				});
			},
			setUnReadMessageCount(count) {
				if (count > 0) {
					uni.setTabBarBadge({
						index: constant.messageIndex,
						text: '' + count
					})
				} else {
					uni.removeTabBarBadge({
						index: constant.messageIndex
					})
				}
			},
		}
	}
</script>

<style lang="scss">
	@import 'static/css/globa.scss';
	
	page {
		position: relative;
		height: 100vh;
		overflow: hidden;
		background-color: #000000;
	}
	
	page::before {
		content: '';
		background-image: url('@/static/img/bg_header2.png');
		//background-image: url('https://zhiyuapp.oss-cn-fuzhou.aliyuncs.com/uniapp_android/image/test/background.png');
		position: absolute;
		top: 0rpx;
		height: 292rpx;
		width: 100%;
		right: 0rpx;
		// bottom: 0rpx;
		left: 0rpx;
		background-size: cover;
		background-clip: padding-box;
		// filter: blur(16rpx);
		// overflow: hidden;
		background-blend-mode: darken;
		z-index: 0;
		// transform: scale(1.4);
	}
</style>